package org.example.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.dto.ClassDto;
import org.example.dto.ClassroomDto;
import org.example.service.ClassroomService;
import org.example.service.TestService;
import org.example.utils.JsonUtils;
import org.example.vo.ClassVo;
import org.example.vo.ClassroomVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Rongchang Wu
 * @version 1.0
 * @description: TODO
 * @date 2021/12/9 16:38
 */
@RestController
public class ClassroomController {
    @Resource
    ClassroomService service;

    @GetMapping("/getClassroomList")
    public void getClassroomList(HttpServletResponse resp, Integer page, Integer limit) throws IOException {
        Page<Object> obj = PageHelper.startPage(page, limit);
        List<ClassroomDto> classroomList = service.queryAllClassroom();
        Integer count = service.countClassroom();
        //以上调用service获取专业数据和数量

        JsonUtils.customJson(resp, classroomList, count);
    }

    @GetMapping("/getClassroomByBuildingName")
    public void getClassroomByBuildingName(HttpServletResponse resp, String buildingName, Integer page, Integer limit) throws IOException {
        Page<Object> obj = PageHelper.startPage(page, limit);
        List<ClassroomDto> classroomList = service.queryClassroomByBuilding(buildingName);
        Integer count = service.countClassroomByBuilding(buildingName);

        JsonUtils.customJson(resp, classroomList, count);
    }

    @GetMapping("/getClassroomByBuildingNameAndClassroomType")
    public void getClassroomByBuildingNameAndClassroomType(HttpServletResponse resp, String buildingName, String classroomType, Integer page, Integer limit) throws IOException {
        Page<Object> obj = PageHelper.startPage(page, limit);
        List<ClassroomDto> classroomList = service.queryClassroomByBuildingAndClassroomType(buildingName, classroomType);
        Integer count = service.countClassroomByBuildingAndClassroomType(buildingName, classroomType);

        JsonUtils.customJson(resp, classroomList, count);
    }

    @GetMapping(value = "/deleteClassroom", produces = "text/plain;charset=utf-8")
    public String deleteClassroom(String classroomId) {
        String[] classroomIds = classroomId.split(",");
        Integer status = 0;
        for (int i = 0; i < classroomIds.length; i++) {
            status += service.deleteClassroom(Integer.parseInt(classroomIds[i]));
        }

        return status > 0 ? "success" : "error";
    }

    @GetMapping(value = "/addClassroom", produces = "text/plain;charset=utf-8")
    public String addClassroom(ClassroomVo classroom) {
        System.out.println("进入/addClassroom");
        Integer status = service.addClassroom(classroom);

        return status > 0 ? "success" : "error";
    }

    @GetMapping(value = "/getClassroomById")
    public ClassroomDto getClassroomById(Integer classroomId) {
        ClassroomDto classroomDto = service.queryClassroomById(classroomId);

        return classroomDto;
    }

    @GetMapping(value = "/editClassroom")
    public String editClassroom(ClassroomVo classroom) {
        Integer status = service.updateClassroom(classroom);

        return status > 0 ? "success" : "error";
    }
}
